﻿using Core.Framework;
using System;

namespace DotNetty.Framework.BaseTriggers
{
    public class WorkerController
    {
        public int SecondOfTimeOut { get; set; } = 10;

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        //protected bool GetClusterClientAsync(out IClusterClient cluster)
        //{
        //    var client = ProviderOfServices.GetService<OrleansClientConnaction>();

        //    var isActive = client.IsServiceActive(out cluster);

        //    return isActive;
        //    //return (isActive, clusterOfOut);

        //    //var task = Task.Run(() => { 
        //    //    var isActive = client.IsServiceActive(out IClusterClient clusterOfOut);
        //    //    return (isActive, clusterOfOut);
        //    //});

        //    //task.ConfigureAwait(false);

        //    //var taskResult = task.Result;

        //    //cluster = taskResult.clusterOfOut;

        //    //return taskResult.isActive;
        //    //return client.StartConnectAsync();
        //}

        //protected async Task<DoWorkResponse> InvokeByCluster(Func<IClusterClient, Task<DoWorkResponse>> action, HandleDoWorkingContext context)
        //{
        //    if (this.GetClusterClientAsync(out IClusterClient cluster))
        //    {
        //        var result = await action.Invoke(cluster);

        //        return this.GetWorkResponse(context, result);
        //    }
        //    else
        //    {
        //        return this.GetErrorResponseOfOrleansNotActive(context);
        //    }
        //}

        /// <summary>
        /// 获取返回值
        /// </summary>
        /// <param name="context"></param>
        /// <param name="body">返回参数</param>
        /// <returns></returns>
        protected DoWorkResponse GetWorkResponse(HandleDoWorkingContext context, object body)
        {
            var response = new DoWorkResponse();
            response.Token = context.Message.Token;
            response.Command = context.Message.CommandText;
            response.Code = CodeResult.SUCCESS;
            response.Body = body;
            response.TaskRunTimeOfMilliseconds = this.GetTimeDifference(context.Message);

            return response;
        }

        protected DoWorkResponse GetWorkResponse(
            HandleDoWorkingContext context,
            string twtToken,
            bool isOutLogin,
            object body)
        {
            var response = new DoWorkResponse();
            response.Token = context.Message.Token;
            response.Command = context.Message.CommandText;
            response.Code = CodeResult.SUCCESS;
            response.Body = body;
            response.WSToken = twtToken;
            response.IsOutLogin = isOutLogin;
            response.TaskRunTimeOfMilliseconds = this.GetTimeDifference(context.Message);

            return response;
        }


        /// <summary>
        /// 获取返回值 参数验证失败
        /// </summary>
        /// <param name="context"></param>
        /// <param name="error">异常信息</param>
        /// <returns></returns>
        protected DoWorkResponse GetErrorResponseOfParameterValidationFailed(HandleDoWorkingContext context, string error)
        {
            var response = new DoWorkResponse();
            response.Token = context.Message.Token;
            response.Command = context.Message.CommandText;
            response.Code = CodeResult.PARAMS_IS_INVALID;
            response.RetryDelayedOfSecond = 0;
            response.Info = error;
            response.TaskRunTimeOfMilliseconds = this.GetTimeDifference(context.Message);
            return response;
        }

        /// <summary>
        /// 获取 Orleans 失活 返回值
        /// </summary>
        /// <param name="context"></param>
        /// <param name="body">返回参数</param>
        /// <returns></returns>
        protected DoWorkResponse GetErrorResponseOfOrleansNotActive(HandleDoWorkingContext context)
        {
            var response = new DoWorkResponse();
            response.Token = context.Message.Token;
            response.Command = context.Message.CommandText;
            response.Code = CodeResult.INTERFACE_EXCEED_LOAD;
            response.RetryDelayedOfSecond = (2 + context.Message.RetryCount);
            response.RetryCount = (context.Message.RetryCount + 1);
            response.TaskRunTimeOfMilliseconds = this.GetTimeDifference(context.Message);
            return response;
        }

        private double GetTimeDifference(AbstractMessage message)
        {
            // 利用TimeSpan计算时间差
            TimeSpan ts1 = new TimeSpan(message.TaskStartDateTime.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
            return ts2.Subtract(ts1).TotalMilliseconds;
        }

    }
}
